package cn.itcast.web.controller;



import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.Encrypt;
import com.alibaba.excel.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class LoginController extends BaseController{
    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;

    /*//用户登陆
	@RequestMapping("/login")
	public String login(String email,String password) {
        //1、判断email和password是否为null
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            //跳转到登录页面，重新登录
            request.setAttribute("error", "用户名或者密码不能为空");
            return "forward:/login.jsp";
        }

        //2、根据email查询用户
        User user = userService.findByEmail(email);
        //3、判断用户对象是否存在，且密码是否一致
        //对输入的登录密码加密
        password = Encrypt.md5(password,email);

        if(user !=null && user.getPassword().equals(password)) {
            //3.1 用户存在且密码一致：登录成功

           //根据当前登录的用户，查询可操作性的所有模块
            List <Module> list = moduleService.findByUser(user);
            session.setAttribute("modules",list);

            //将用户数据存入session
            session.setAttribute("loginUser",user);
            //跳转主页
            return "home/main";
        }else {
            //3.2 用户不存在或密码不一致：邮箱或者密码错误
            //重新登录，携带一些错误信息
            request.setAttribute("error", "邮箱或者密码错误");
            return "forward:/login.jsp";
        }

	}*/

    //基于shiro的用户认证
    @RequestMapping("/login")
    public String login(String email,String password) {
        //1、判断email和password是否为null
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            //跳转到登录页面，重新登录
            request.setAttribute("error", "用户名或者密码不能为空");
            return "forward:/login.jsp";
        }
        //2、对输入的登录密码加密
        password = Encrypt.md5(password,email);

        try {
            //3、获取工具类subject
            Subject subject = SecurityUtils.getSubject();
            //4、调用subject的login方法进入realm完成认证
            UsernamePasswordToken upToken = new UsernamePasswordToken(email,password);
            subject.login(upToken);
            //5、如果正常执行，无异常（登录成功）
            //从shiro获取安全数据
            User user = (User) subject.getPrincipal(); //安全数据（用户）
            //根据当前登录的用户，查询可操作性的所有模块
            List <Module> list = moduleService.findByUser(user);
            session.setAttribute("modules",list);
            //将用户数据存入session
            session.setAttribute("loginUser",user);
            //跳转主页
            return "home/main";
        }catch (Exception e) {
            e.printStackTrace();
            //6、如果抛出异常（登录失败）
            request.setAttribute("error", "邮箱或者密码错误");
            return "forward:/login.jsp";
        }
    }

    //退出
    @RequestMapping(value = "/logout",name="用户退出")
    public String logout(){
        //SecurityUtils.getSubject().logout();   //退出
        Subject subject = SecurityUtils.getSubject();
        subject.logout();//用户退出登录清空数据
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
